package com.sendi.system.util.common;

import java.io.UnsupportedEncodingException;

 
public class StringUtil {

	
	
	/*
	 * 编码为utf-8
	 */
	public static String encode(String a)
	{
		try {
			String x=a==null||a.trim().length()==0?"":a;
			String xencode=getEncoding(x);
			if(xencode.equalsIgnoreCase("ISO8859_1"))
			{
				String tt=new String(x.getBytes("ISO-8859-1"),"gbk");
				String encode=getEncoding(tt);
				String ISOtoUTF="";
				if(encode.equalsIgnoreCase("GBK")||encode.equalsIgnoreCase("GB2312"))//如果能够转码为gbk的话,还是能拿到GBK的.但如果转码不正确,则还是原来的编码,即使做了转码工作
				{
					ISOtoUTF=new String(gbk2utf8(tt),"utf-8");	
				}
				else
				{
					ISOtoUTF=new String(x.getBytes("ISO-8859-1"),"UTF-8");
				}
			
		 		return   ISOtoUTF;
			}
			else if(xencode.equalsIgnoreCase("UTF-8"))
			{
				 
		 	 	return   x;
			}
			else if(xencode.equalsIgnoreCase("GBK")||xencode.equalsIgnoreCase("GB2312"))
			{
				String GbtoUtf=new String(gbk2utf8(x),"utf-8");
				return GbtoUtf;
			}
			else
			{
				return "";
			}
		 
	 	 	 
		} catch (UnsupportedEncodingException e) {
		 
			e.printStackTrace();
			return a;
		}
			
	}
	
	

	/*
	 * 获取字符集编码
	 */
	public  static String getEncoding(String content)
	{
		String encodeName = "";
		byte[] temp;  
		String compstring;  
		try {
			temp = content.getBytes("ISO-8859-1");
			compstring = new String(temp,"ISO-8859-1");  
			if(content.equals(compstring))  
			{
 
				encodeName = "ISO-8859-1";
			}
			else
			{
 
				temp = content.getBytes("GB2312");
				compstring = new String(temp,"GB2312");  
				if(content.equals(compstring))  
				{
 
					encodeName = "GB2312";
				}
				else
				{
					temp = content.getBytes("UTF-8");
					compstring = new String(temp,"UTF-8");  
					if(content.equals(compstring))  
					{
 
						encodeName = "UTF-8";
					}
 
				}
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}  
		return encodeName;
	}
 

	public static String getSortFieldType(String filedtype)
	{
		if(filedtype.equalsIgnoreCase("INT")||filedtype.equalsIgnoreCase("LONG")||filedtype.equalsIgnoreCase("DOUBLE")||filedtype.equalsIgnoreCase("FLOAT"))
		{
			return "NUMBER";
		}
		else if(filedtype.equalsIgnoreCase("DATE"))
		{
			return "DATE";
		}
		else
			return "STRING";
	}
	
	/*
	 * 替换终端特殊控制字符
	 */
	public static String ReplaceTerminalSpecialChar(String terminalString)
	{
		 char [] k=terminalString.toCharArray();
		 StringBuffer sb=new StringBuffer("");
		 for(char c:k)
		 {
			 int specik=(int)c;
			 if(specik>=32||specik==13||specik==10||specik==9)
			 {
				 sb.append(c);
			 }
		 }
		 
		 return sb.toString();
	}
	
	 public static byte[] gbk2utf8(String chenese){
		  char c[] = chenese.toCharArray();
		        byte [] fullByte =new byte[3*c.length];
		        for(int i=0; i<c.length; i++){
		         int m = (int)c[i];
		         String word = Integer.toBinaryString(m);

		         
		         StringBuffer sb = new StringBuffer();
		         int len = 16 - word.length();
		         //补零
		         for(int j=0; j<len; j++){
		          sb.append("0");
		         }
		         sb.append(word);
		         sb.insert(0, "1110");
		         sb.insert(8, "10");
		         sb.insert(16, "10");
	         
		         String s1 = sb.substring(0, 8);          
		         String s2 = sb.substring(8, 16);          
		         String s3 = sb.substring(16);
		         
		         byte b0 = Integer.valueOf(s1, 2).byteValue();
		         byte b1 = Integer.valueOf(s2, 2).byteValue();
		         byte b2 = Integer.valueOf(s3, 2).byteValue();
		         byte[] bf = new byte[3];
		         bf[0] = b0;
		         fullByte[i*3] = bf[0];
		         bf[1] = b1;
		         fullByte[i*3+1] = bf[1];
		         bf[2] = b2;
		         fullByte[i*3+2] = bf[2];
		         
		        }
		        return fullByte;
		 }

	
}
